/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

// MusicBoxDlg.h : header file
//
#pragma once
#include <MMSystem.h>
#include <comdef.h>
#include <gdiplus.h>    
#include <dshow.h>
#include <memory>
#include "SimpleTray.h"
#include "SliderCtrlEx.h"
#include "afxcmn.h"

// Player's status
enum PlayerStatus {Player_None, Player_Running, Player_Stopped, Player_Paused};

// CMusicBoxDlg dialog
class CMusicBoxDlg : public CDialog
{

public:
    // Construction
	CMusicBoxDlg(CWnd* pParent = NULL);
    ~CMusicBoxDlg();

    // Dialog Data
	enum { IDD = IDD_MUSICBOX_DLG };

	// Message handler 
    afx_msg void OnBnClickedOpenBtn();
    afx_msg void OnBnClickedStopBtn();
    afx_msg void OnBnClickedNextBtn();
    afx_msg void OnBnClickedStartPauseBtn();
    afx_msg void OnBnClickedMute();
    afx_msg void OnBnClickedLrcShow();
    afx_msg void OnBnClickedPlShow();
    afx_msg void OnBnClickedPrevBtn();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg void OnDestroy();
    afx_msg void OnClose();
    // Right click menu handler
    afx_msg void OnPlayerOption();
    afx_msg void OnPlayerMin();
    afx_msg void OnPlayerExit();
    // User defined message handler
    // System tray message handler
	afx_msg LRESULT OnTrayNotify(WPARAM wParam, LPARAM lParam);
    // Play list message handler
    // Play media
    afx_msg LRESULT OnPLPlayMedia(WPARAM wParam, LPARAM lParam);
    // Show main window
    void OnShowMainWin();
    // Block ESC and Enter to prevent dialog exit
    BOOL PreTranslateMessage(MSG* pMsg);
protected:
    // DDX/DDV support
	virtual void DoDataExchange(CDataExchange* pDX);
	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/);
	afx_msg void OnPaint();

	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

    // User defined message map functions
	afx_msg LRESULT OnGraphEvent(WPARAM wParam, LPARAM lParam);

private:
    // Helper functions
    // Direct show helper functions
    // Init the dshow coms
    HRESULT InitDirectShow();
    // Free the dshow coms
    HRESULT FreeDirectShow();
    // Reset the dshow coms(call freedirect show & initdirctshow)
    void ResetDirectShow();
    // Media helper functions: play, pause, stop
    bool PlayMedia(const CString & SongPath);
    bool PauseMedia();
    bool StopMedia();
    HRESULT PrepareMedia(const CString & SongPath);
    // Configure controls in different mode(play, pause, stop)
    // Set title etc.
    void ConfigPlayCtrls();
    void ConfigStopCtrls();
    void ConfigPauseCtrls();
    // Timer operatation
    void StartChgTitleTimer();
    void StopChgTitleTimer();
    void StartSeekPosTimer();
    void StopSeekPosTimer();
    // On time callback
    static void CALLBACK SeekPosTimeOut(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
    static void CALLBACK ChgTitleTimeOut(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
    // Do real job, show current position and change title property
    void ShowCurPos();
    void ChgTitle();

    // Gragh operation functions
    HRESULT RunGraph();
    HRESULT StopGraph();
    HRESULT PauseGraph();
    // Audio helper functions
    HRESULT Mute();
    HRESULT UnMute();
    HRESULT SetVolume(long Volume);

    // Working thread for read song's property, get lrc, start lrc show
    static unsigned int WINAPI ThreadOnOpen(void* Param);  
	void OpenNewSong(int SongIdx, int PLIdx=-1);                

    // Handle seek position bar message
    // Play progress
    void HandleSeekPosBar(UINT SBCode);
    // Volume
    void HandleVolumeSlider(UINT SBCode);

    // Data member
    // System tray icon
    HICON m_hIcon;
    CSimpleTray    m_SysTray;                 // System tray
    // DShow interfaces
    IGraphBuilder* m_GraphBuilder;
    IMediaSeeking* m_MediaSeek;
    IMediaControl* m_MediaControl;
    IMediaEventEx* m_MediaEvent;
    // Gdi plus data member
    Gdiplus::GdiplusStartupInput m_GdiplusStartupInput;
    ULONG_PTR      m_GdiplusToken;
    // Timer handler
    MMRESULT m_PosTimer;                      // Synchronal timer for seeking current position
    MMRESULT m_TitleTimer;                    // Synchronal timer for changing current song's property(title, singer, duration, format)
    // Mode
    PlayerStatus m_PlayStatus;                // Status of current song(stop, pause, play)
    // Control handler 
    CSliederCtrlEx m_SeekPosBar;              // Media position seek bar
    CSliederCtrlEx m_VolCtrl;                 // Volumn control seek bar
    CMenu m_RClkMenu;                         // Mouse right click menu
};
